package com.xh.car.controller;

import com.xh.car.bean.Car;
import com.xh.car.service.CarService;
import com.xh.car.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * @Author PangJunjie
 * @Date 2024/5/13/013
 */
@Controller
@RequestMapping("/car")
public class CarController {
    @Autowired
    private CarService carService;

    @GetMapping("")
    public String index() {
        return "car/car-list";
    }

    @GetMapping("/edit")
    public String edit(Integer id, Model model) {
        if (id != null) {
            model.addAttribute("car", carService.getById(id));
        }
        return "car/car-edit";
    }

    @GetMapping("/page")
    @ResponseBody
    public R page(int page, int limit, Car car) {
        return carService.getPage(page, limit, car);
    }

    @PostMapping(value = {"/update", "/status"})
    @ResponseBody
    public R update(Car car) {
        carService.saveOrUpdate(car);
        return R.ok();
    }

    @DeleteMapping("/delete/{id}")
    @ResponseBody
    public R delete(@PathVariable int id) {
        carService.deleteById(id);
        return R.ok();
    }

    @GetMapping("/select")
    @ResponseBody
    public R select() {
        return carService.getSelect();
    }

    @GetMapping("/brand/select")
    @ResponseBody
    public R brandSelect(@RequestParam(value = "pid", defaultValue = "0") int pid) {
        return carService.getBranList(pid);
    }

    @GetMapping("/image/{id}")
    @ResponseBody
    public R carImage(@PathVariable int id) {
        return carService.getCarImage(id);
    }
}
